package com.dao;
import java.util.List;
import java.util.Set;

import com.hbt.HibernateUtil;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import com.bean.OrdenCompra;
import com.bean.Proveedor;

public class ProveedorDAO {

	private static SessionFactory sf = null;
	private static ProveedorDAO instancia = null;
	
	
	public static ProveedorDAO getInstancia(){
		if(instancia == null){
			sf = HibernateUtil.getSessionFactory();
			instancia = new ProveedorDAO();
		}
		return instancia;
	}
	
	public boolean validar(Proveedor prov){
		Session session = sf.openSession();
		OrdenCompra oc = (OrdenCompra) session.createQuery("from OrdenCompra oc where oc.proveedor.cuit = :var")
		.setString("var", prov.getCuit()).setFirstResult(0).setMaxResults(1).uniqueResult();
		session.close();
		if ( oc == null )
			return true;
		return false;
	}
	
	public void grabarProveedor(Proveedor prov){
		Session session = sf.openSession();
		session.beginTransaction();
		session.save(prov);
		session.getTransaction().commit();
		session.close();
	}
	
	public void grabarProveedores (Set <Proveedor> proveedores){
		Session session = sf.openSession();
		session.beginTransaction();
		for (Proveedor p : proveedores){
			session.save(p);
		}
		session.getTransaction().commit();
		session.close();
	}
	
	public Proveedor getProveedor(Proveedor prov) {
		Session session = sf.openSession();
		Proveedor proveedor = (Proveedor) session.get(Proveedor.class, prov.getId());
		session.close();
		return proveedor;
	}
	
	@SuppressWarnings("unchecked")
	public List<Proveedor> getProveedores(){
		Session session = sf.openSession();
		List<Proveedor> lst = session.createQuery("from Proveedor").list();
		session.close();
		return lst;	
	}
	
	public Proveedor getProveedor(String cuit){
		Session session = sf.openSession();
		Proveedor prov = (Proveedor) session.createQuery("from Proveedor p where p.cuit = :var")
		.setString("var", cuit).setFirstResult(0).setMaxResults(1).uniqueResult();
		session.close();
		return prov;
	}


	public void bajaProveedor(Integer id) {
		Session session = null;
		try{
			session = sf.openSession();	
			session.beginTransaction();
			//Query query = session.createQuery("Delete Proveedor p where p.id = :var").setInteger("var", id);
			//query.executeUpdate();
			Proveedor p = (Proveedor) session.get(Proveedor.class, id);
			session.delete(p);
			session.getTransaction().commit();
			session.close();
				
		}catch (HibernateException e) {
			System.out.println("Imposible borrar. Proveedor en uso.");
			if (session != null)
				session.close();
		}
		
	}

	public void actualizarProveedor(Proveedor prov) {
		try{
			
			Session session = sf.openSession();
			session.beginTransaction();
			session.update(prov);
			session.getTransaction().commit();
			session.close();		
		}catch (Exception e) {
			System.out.println(e.getMessage());
		}
	}
	
}
